package com.fancy.learncenter.view;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fancy.learncenter.adapter.ConditionAdapter;
import com.fancy.learncenter.adapter.GoodsTypeAdapter;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.EducationDataBean;
import com.fancy.learncenter.bean.GoodsTypeDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**s
 * Created by hyy on 2018/2/25.
 * Explain activity as
 */

public class PopupWindowExperienceType extends BasedPopupWindow {
    View locationView;

    View popopView;
    View viewParent;

    RecyclerView recyclerView;

    Activity mContext;

    TextView confrim;

    public PopupWindowExperienceType(Activity mContext, View viewParent) {
        super(mContext);
        this.mContext = mContext;
        this.viewParent = viewParent;
        this.locationView = locationView;
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.pupopwinodw_salary, null);
        initPopupWionw();

        getDatas();
    }

    private void initPopupWionw() {
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//        this.setOutsideTouchable(true);

        recyclerView = (RecyclerView) popopView.findViewById(R.id.recycleView);
        confrim = popopView.findViewById(R.id.confrim);
        confrim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (clickCallBack != null) {
                    clickCallBack.selecCallBack(cutItem.getSalary_name(), cutItem.getId() + "");
                }

            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);


        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindowExperienceType.this.dismiss();
            }
        });

        this.update();

        this.setContentView(popopView);

        commonRecycleViewAdapter = new CommonRecycleViewAdapter<EducationDataBean>(mContext, R.layout.pupopwinodw_condition_text_item, new ArrayList<EducationDataBean>()) {
            @Override
            public void bindView(CustomViewHold customViewHold, final EducationDataBean item, final int position) {
                TextView content = customViewHold.getView(R.id.content);
                content.setText(item.getSalary_name());

                if (item.isSelect()) {
                    content.setBackgroundResource(R.color.orange);
                    content.setTextColor(Color.parseColor("#FFFFFF"));
                } else {
                    content.setBackgroundResource(R.color.white);
                    content.setTextColor(Color.parseColor("#3f4344"));
                }

                customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        getDatas().get(oldSelect).setSelect(false);
                        getDatas().get(position).setSelect(true);
                        notifyItemChanged(oldSelect);
                        notifyItemChanged(position);

                        oldSelect = position;


                        cutItem = item;
                    }
                });
            }
        };
        recyclerView.setAdapter(commonRecycleViewAdapter);

    }

    public EducationDataBean cutItem;
    private int oldSelect = 0;

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        showAsDropDown(viewParent);
    }

    public void setClickCallBack(PopupWindowEducationType.ClickCallBack clickCallBack) {
        this.clickCallBack = clickCallBack;

    }

    private CommonRecycleViewAdapter<EducationDataBean> commonRecycleViewAdapter;

    @Override
    public void dismiss() {
        super.dismiss();
        if (clickCallBack != null) {
            clickCallBack.cancleCallBack();
        }
    }

    private PopupWindowEducationType.ClickCallBack clickCallBack;

    public interface ClickCallBack {
        abstract void cancleCallBack();

        abstract void selecCallBack(String content, String id);
    }

    List<EducationDataBean> itemDatas;

    private void getDatas() {
        Map map = new HashMap();
        HttpMehtod.getInstance().getExperienceType(map, new IdeaObserver<BaseDataBean<List<EducationDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<EducationDataBean>> listBaseDataBean) {

                commonRecycleViewAdapter.notifyDataSetChanged((ArrayList<EducationDataBean>) listBaseDataBean.getData());
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
